<?php
require $_SERVER['DOCUMENT_ROOT'].'/../_core/common.php';

//权限验证
if( Role::init() == false ){
	exit('没有权限');
}

$db = new DB('news');

$category_id = isset($_POST['parent_id']) ? intval($_POST['parent_id']) : 0;
$title = isset($_POST['title']) ? h($_POST['title']) : '';
$url = isset($_POST['url']) ? h($_POST['url']) : '';
$views = (isset($_POST['views']) && $_POST['views']!='') ? intval($_POST['views']) : rand(100,200);
$create_uid = Sessions::get('uid');
$edit_uid = $create_uid;
$ip = IP::get();
$created = time();
$updated = $created;

//检验必要参数
if( $category_id <= 0 || $title == '' || $url == '' || substr($url, 0, 7) != 'http://' ){
	echo '<script>alert("参数有误！");window.history.go(-1);</script>';
	exit;
}

//生成当前title_id
$category_sql = 'select title_id,counts from categories where id="'.$category_id.'" and is_del=1 limit 1';
$category_rs = $db->query($category_sql)->getOne();
if($category_rs['title_id']>0){
	$title_id = $category_id.'-'.($category_rs['title_id']+1);
}else{
	$title_id = $category_id.'-1';
}

//根据id查找所有父级数据数组
function parent_check($db,$id){
	global $category_arr;
	$sql = 'select parent_id from categories where id='.$id.' limit 1';
	$data = $db->query($sql)->getOne();
	
	array_push($category_arr, $data);
	
	if($data['parent_id']!=0){
		parent_check($db,$data['parent_id']);
	}
}
//组装静态地址
$static_url = 'http://news.zgqw.com';
$category_arr = array();
parent_check($db,$category_id);
array_pop($category_arr);
array_unshift($category_arr, array('parent_id'=>$category_id));
$category_arr = array_reverse($category_arr);
foreach($category_arr as $arr){
	$static_url .= '/'.$arr['parent_id'];
}
$static_url .= '/'.$title_id;

//解析得到原文章的title_id
$empty_id = '1';
if( substr($url, 0, 21) == 'http://news.zgqw.com/' ){
	$this_url = substr($url, 21);
	//   1/25/25-2270/    article.php?title_id=30-10&page=2
	$this_url_array = explode($this_url, 'title_id=');
	
	if( count($this_url_array) == 2 ){
		$title_id_array = explode($this_url_array[1], '&');
		$empty_id = $title_id_array[0];
	}else{
		preg_match('/^(\d+\/)+(\d+-\d+)(-\d+){0,1}\/{0,1}$/', $this_url, $title_id_array);
		$empty_id = isset($title_id_array[2]) ? $title_id_array[2] : '1';
	}
}

//组装news的insert数组
$news_data=array('title_id'=>$title_id,
				'category_id'=>$category_id,
				'title'=>$title,
				'url'=>$url,
				'static_url'=>$static_url,
				'views'=>$views,
				'title_history_id'=>1,
				'create_uid'=>$create_uid,
				'edit_uid'=>$edit_uid,
				'ip'=>$ip,
				'empty_id'=>$empty_id,
				'created'=>$created,
				'updated'=>$updated,
				);
$db->table('news')->insert($news_data)->run();//插入

//更新栏目title_id、counts字段
$update_data = array('title_id'=>($category_rs['title_id']+1), 'counts'=>($category_rs['counts']+1));
$db->table('categories')->where('id='.$category_id)->update($update_data)->run();

//记录动作日志
Log::logs_articles($category_id, 14, $title_id, $title);

$error_title = '添加成功 - 添加空白文章';
$error_content = '添加成功！';
$error_pre_button = '<a href="/admin/news/news_lists.php?category_id='.$category_id.'" style="margin-right:20px;" id="pre_button">〈〈 返回列表</a>';
$error_next_button = '<a href="/admin/news/empty_add.php?category_id='.$category_id.'">继续添加 〉〉</a>';
require template('admin/news/news_error');
exit;